<template>
  <div class="first-screen">
    <img v-show="state.bgImgUrl" :src="state.bgImgUrl" style="width: 100%; height: 100%; object-fit: cover" />
    <div class="content-wrap" :class="{ showContent: state.isShowContent }">
      <img class="avatar pointer" :src="state.avatar" @click="handleTo('/about/me')" />
      <div class="saying box-shadow">
        <div class="saying-text text">
          <span class="saying-icon">“</span>
          <span class="saying-words">{{ state.saying }}</span>
          <span class="saying-write">|</span>
          <span class="saying-icon">”</span>
        </div>
        <h5 class="text" style="margin-top: 15px">Hello~ 我是 冬七</h5>
      </div>
      <div class="author-info">
        <div class="info-item tool-btn kbn-custom" data-tip="切换 「上一张背景」！" :class="{ 'm-tool-btn': isMobi, 'box-shadow': !isMobi }" @click="handleToggleHomeBg('pre')">
          <img class="btn" src="@/assets/images/icon/pre.png" alt="" srcset="" />
        </div>
        <div class="info-out-wrap-scroll">
          <div class="info-item-out-wrap" :class="{ 'is-tool-next': state.isToolNext }">
            <div class="info-item-wrap" v-for="(infoitemList, index) in infoList" :key="index">
              <div v-for="(item, index) in infoitemList" :key="index" class="info-item box-shadow" :class="item.class" :data-tip="item.kbnTip" @click="handleInfo(item)">
                <template v-for="img in item.imgs">
                  <img class="img" :class="img.class" :src="img.img" alt="" srcset="" />
                </template>
              </div>
            </div>
          </div>
        </div>
        <div class="info-item tool-btn kbn-custom" data-tip="切换 「下一张背景」!" :class="{ 'm-tool-btn': isMobi, 'box-shadow': !isMobi }" @click="handleToggleHomeBg('next')">
          <img class="btn" src="@/assets/images/icon/next.png" alt="" srcset="" />
        </div>
      </div>
    </div>
    <div class="bottom-down" @click="handleMoveToNext">
      <IconDown></IconDown>
    </div>
    <div class="bottom-bg"></div>
    <div class="bottom-bg1"></div>
    <div class="bottom-bg1"></div>
    <div class="transition-wrap"></div>
    <div class="mengce"></div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import { getOnlineImgUrl, getQQAvatar } from '@/utils/common';
import { useAppStore } from '@/store/app';
import useResize from '@/hooks/useResize';
import iconEmail from '@/assets/images/icon/mail.png';
import iconQQ from '@/assets/images/icon/qq.png';
import iconWeixin from '@/assets/images/icon/weixin.png';

const router = useRouter();
const store = useAppStore();
const { isMobi } = useResize();

const infoList = [
  [
    {
      class: 'kbn-chart',
      imgs: [{ img: iconQQ }],
      kbnTip: 'QQ: 37523953',
      link: false,
      fnQuery: 'qq'
    },
    {
      class: 'weixin-content kbn-chart',
      imgs: [
        { class: 'weixin-icon', img: iconWeixin },
        { class: 'weinxin-qrcode', img: getOnlineImgUrl('wechat-qrcode', '/imgs/common', 'png') }
      ],
      kbnTip: 'WeChat: snows__l',
      link: false,
      fnQuery: 'weixin'
    },
    {
      class: 'kbn-chart',
      imgs: [{ img: iconEmail }],
      kbnTip: 'Email: snows_l@163.com',
      link: true,
      fnQuery: 'email'
    }
  ]
];

const saying = '我见众生皆草木，唯你是青山。';

const state = reactive({
  saying: '',
  bgImgUrl: getOnlineImgUrl(store.projectConfig.homeDefaultBg),
  isShowContent: false,
  avatar: getQQAvatar()
});

// 打字效果输出saying
let intervalTimer: number | null = null;
const inputSaying = () => {
  let index = 0;
  intervalTimer = setInterval(() => {
    if (index === saying.length && intervalTimer) {
      clearInterval(intervalTimer);
      intervalTimer = null;
      return;
    }
    state.saying += saying[index];
    index++;
  }, 120);
};

// 切换首页背景
const handleToggleHomeBg = (type: string) => {
  if (type === 'next') {
    if (store.currentBgName == store.projectConfig.bgImgEnd) {
      store.SET_BG_INDEX(store.projectConfig.bgImgStart);
    } else {
      store.SET_BG_INDEX(store.currentBgName + 1);
    }
  } else {
    if (store.currentBgName == store.projectConfig.bgImgStart) {
      store.SET_BG_INDEX(store.projectConfig.bgImgEnd);
    } else {
      store.SET_BG_INDEX(store.currentBgName - 1);
    }
  }
  state.bgImgUrl = getOnlineImgUrl(store.currentBgName);
};

// infoMap
const infoMap = {
  qq: 'mqqwpa://im/proxy?src_type=web&qq_number=37523953',
  email: 'mailto:snows_l@163.com'
};
// 跳转到QQ
const toQQ = () => {
  let system = {};
  const p = navigator.platform;
  system.win = p.indexOf('Win') == 0;
  system.mac = p.indexOf('Mac') == 0;
  system.x11 = p == 'X11' || p.indexOf('Linux') == 0;
  if (system.win || system.mac || system.xll) {
    //如果是电脑跳转到
    window.open('http://wpa.qq.com/msgrd?v=3&uin=37523953&site=qq&menu=yes');
  } else {
    //如果是手机,跳转到
    window.open('mqqwpa://im/chat?chat_type=wpa&uin=37523953&version=1&src_type=web');
  }
};

const handleInfo = (item: { class: string; imgs: any[]; kbnTip: string; title: string; link: boolean; fnQuery: string }) => {
  if (item.link) {
    window.open(item.link, '_blank');
  } else {
    if (item.fnQuery === 'qq') {
      toQQ();
    } else {
      infoMap[item.fnQuery] && window.open(infoMap[item.fnQuery]);
    }
  }
};

// 下一页
const handleMoveToNext = () => {
  if (document.getElementById('layout')) {
    document.getElementById('layout').scrollTop = document.getElementById('layout')?.clientHeight - 60;
  }
};

// 跳转关于我
const handleTo = (path: string) => {
  router.push({
    path
  });
};

let timer: null | number = null;
let timer2: null | number = null;
onMounted(() => {
  timer = setTimeout(() => {
    state.isShowContent = true;
  }, 400);

  timer2 = setTimeout(() => {
    inputSaying();
  }, 800);
});

// 卸载 优化
onUnmounted(() => {
  if (timer) {
    clearTimeout(timer);
    timer = null;
  }

  if (timer2) {
    clearTimeout(timer2);
    timer2 = null;
  }

  if (intervalTimer) {
    clearInterval(intervalTimer);
    intervalTimer = null;
  }
});
</script>

<style lang="scss" scoped>
.first-screen {
  width: 100%;
  height: 100vh;
  position: relative;
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-color: var(--bg-image-wrap-color);
  .content-wrap {
    max-width: 96%;
    width: 60%;
    margin: 0 auto;
    position: absolute;
    top: 25%;
    left: 50%;
    transform: translate(-50%, 0);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: top 0.8s ease-in, opacity 0.8s ease-in;
    .avatar {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      border: rgba(144, 147, 153, 0.23) 5px solid;
      transition: all 1.2s ease-in-out;
    }
    .avatar:hover {
      border-width: 8px;
      transform: rotate(360deg);
    }
    .saying {
      width: 100%;
      max-width: 750px;
      min-width: 350px;
      padding: 20px 0;
      margin-top: 30px;
      border-radius: var(--border-radius-2);
      background-color: var(--home-saying-color);
      color: var(--text-color);
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .saying-icon {
        margin: 0 10px;
        font-size: 22px;
        font-weight: 800;
      }
      .saying-write {
        font-size: 16px;
        margin: 0 3px;
        font-weight: 600;
        animation: blink 1s infinite;
      }
      .saying-words {
        font-size: 16px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .author-info {
      display: flex;
      margin-top: 30px;
      align-items: center;
      justify-content: center;
      height: 80px;
      .info-out-wrap-scroll {
        width: 230px;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        height: 100%;
        .info-item-out-wrap {
          display: flex;
          justify-content: center;
          align-items: center;
          position: absolute;
          top: 0;
          left: 0;
          height: 100%;
          transition: left 0.8s ease-in-out;
          .info-item-wrap {
            width: 230px;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
          }
        }
        .is-tool-next {
          left: -230px;
        }
      }
      .info-item {
        margin: 0 10px;
        cursor: url('@/assets/images/cursor/pointer.png'), auto;
        padding: 8px;
        border-radius: var(--border-radius-2);
        background-color: var(--bg-wrap-color);
        display: flex;
        justify-content: center;
        align-items: center;
        width: 50px;
        height: 50px;
      }
      .tool-btn {
        margin: 0 5px;
      }
      .m-tool-btn {
        background-color: transparent;
        width: 30px !important;
      }
      .btn,
      img {
        width: 30px;
        height: 30px;
      }
      .weixin-content {
        width: 50px;
        height: 50px;
        transition: width 0.8s ease-in-out, height 0.8s ease-in-out;
        overflow: hidden;
        .weinxin-qrcode {
          display: none;
        }
      }
      .weixin-content:hover {
        width: 80px !important;
        height: 80px;
        .weixin-icon {
          display: none;
        }
        .weinxin-qrcode {
          display: block;
          width: 64px;
          height: 64px;
        }
      }
    }
  }
  .showContent {
    top: 25%;
    opacity: 1;
  }
  .bottom-bg {
    height: 65px;
    position: absolute;
    bottom: 0;
    width: 400%;
    left: -236px;
    z-index: 4;
    transition-duration: 0.4s, 0.4s;
    animation: move2 20s linear infinite;
    background-image: url('@/assets/images/common/wave1.png');
  }
  .bottom-bg1 {
    position: absolute;
    height: 80px;
    width: 400%;
    left: 0;
    bottom: 0;
    z-index: 3;
    opacity: 1;
    transition-duration: 0.4s, 0.4s;
    animation: move2 40s linear infinite;
    background-image: url('@/assets/images/common/wave2.png');
  }
  .transition-wrap {
    position: absolute;
    bottom: -80px;
    height: 80px;
    width: 100%;
    left: 0;
    z-index: 3;
    opacity: 1;
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.6) 60%, rgba(255, 255, 255, 0.1));
    pointer-events: none;
  }
  .bottom-down {
    position: absolute;
    bottom: 80px;
    left: calc(50% - 40px);
    animation: bottomAnimation 1.6s infinite;
  }
  .mengce {
    width: 100%;
    height: 120px;
    position: absolute;
    bottom: -120px;
    background-image: var(--transform-white);
    pointer-events: none;
  }
}

// 光标闪烁效果动画
@keyframes blink {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
// 背景动画
@keyframes move2 {
  100% {
    background-position: -100% 0;
  }
}
// 下滑按钮动画
@keyframes bottomAnimation {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(16px);
  }
  100% {
    transform: translateY(0);
  }
}
</style>
